package tp.pr4.gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;

import java.awt.Cursor;

public class PlayerPanel extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable _inventory;
	private String[] _cabeceras = new String[]
	{ "id", "Description" };

	private GameGUI _game;

	private String[][] _elemToShow = null;

	private JLabel _lblhealthscore;

	public PlayerPanel(GameGUI game)
	{
		this._game = game;
		setBorder(new TitledBorder(null, "Player info", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		setLayout(new BorderLayout(0, 0));

		_lblhealthscore = new JLabel("Health: 100 Score: 0");
		_lblhealthscore.setHorizontalAlignment(SwingConstants.CENTER);
		add(_lblhealthscore, BorderLayout.NORTH);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		add(scrollPane, BorderLayout.CENTER);

		_inventory = new JTable();
		_inventory.setRequestFocusEnabled(false);
		_inventory.setModel(new DefaultTableModel(_elemToShow, this._cabeceras));
		_inventory.getColumnModel().getColumn(0).setResizable(false);
		_inventory.getColumnModel().getColumn(1).setResizable(false);
		_inventory.getColumnModel().getColumn(1).setPreferredWidth(423);
		scrollPane.setViewportView(_inventory);
	}

	public void update()
	{
		this._lblhealthscore.setText("Health: "
				+ this._game.getPlayer().getHealth() + " Score: "
				+ this._game.getPlayer().getPoints() + "");
		String x=this._game.getPlayer().showItems();
		x=x.replaceAll("\n\n", "\n");x=x.replaceAll("\n\n", "\n");
		String[] itemList = x.split("\n");
		if (itemList.length < 1)
			return;
		int init;
		this._elemToShow = new String[itemList.length - 1][2];

		String desc;
		for (int i = 0; i < this._elemToShow.length; i++)
		{
			init = itemList[i + 1].indexOf('=') + 1;
			desc = itemList[i + 1].substring(init);
			String elem = itemList[i + 1].substring(0, init);
			elem = elem.substring(elem.indexOf('[') + 1, elem.indexOf(']'));
			this._elemToShow[i][0] = elem;
			this._elemToShow[i][1] = desc;
		}

		this._inventory.setModel(new DefaultTableModel(_elemToShow,
				this._cabeceras));
		
	}

	public JTable get_inventory()
	{
		return this._inventory;
	}
}